package com.swt.gap.controller;


import com.swt.gap.common.exception.NoMatchedTemplateException;
import com.swt.gap.common.graph.GraphNeo4jVO;
import com.swt.gap.model.vo.FormDataVO;
import com.swt.gap.model.graphmodel.FormFileModelVO;
import com.swt.gap.model.graphmodel.TextFileModelVO;
import com.swt.gap.model.vo.ImportGraphDataVO;
import com.swt.gap.model.vo.ParseEntityParamsVO;
import com.swt.gap.model.vo.ParseEntityVO;
import com.swt.gap.model.vo.graph.DocImportVO;
import com.swt.gap.model.vo.graph.TaskOverviewVO;
import com.swt.gap.service.GraphDataService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.format.DateTimeParseException;
import java.util.List;

@Api(tags = "图谱数据")
@RestController
@RequestMapping("/graph-data")
@Slf4j
public class GraphDataController {

    private final GraphDataService graphDataService;

    public GraphDataController(GraphDataService graphDataService) {
        this.graphDataService = graphDataService;
    }

    @GetMapping("/form-data")
    @ApiOperation(value = "获取表单样例数据", notes = "获取表单样例数据")
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = FormDataVO.class)})
    public ResponseEntity<FormDataVO> getFormFileData(@RequestParam String savedFileName) {
        return ResponseEntity.ok(graphDataService.getFormFileData(savedFileName));
    }

    @GetMapping("/text-data")
    @ApiOperation(value = "获取文本数据", notes = "获取文本数据")
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = String.class)})
    public ResponseEntity<String> getTextFileData(@RequestParam String savedFileName) throws Exception {
        return ResponseEntity.ok(graphDataService.getTextFileData(savedFileName));
    }

    @PostMapping("/text-entity")
    @ApiOperation(value = "要素提取", notes = "从文本中提取要素")
    @ApiImplicitParam(paramType = "body", dataType = "ParseEntityParamsVO", name = "fileInfo", required = true)
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = ParseEntityVO.class)})
    public ResponseEntity<ParseEntityVO> parseEntity(@RequestBody ParseEntityParamsVO fileInfo) {
        return ResponseEntity.ok(graphDataService.parseEntity(fileInfo));
    }

    @PostMapping("/form-data")
    @ApiOperation(value = "表单数据的关联", notes = "表单数据的关联，生成图数据")
    @ApiResponses({@ApiResponse(code = 201, message = "CREATED")})
    public ResponseEntity generateGraphDataByFormFile(@RequestBody FormFileModelVO record) {
        try {
            return ResponseEntity.ok(graphDataService.generateGraphDataByFile(record));
        } catch (DateTimeParseException e) {
            log.error("日期时间格式错误");
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("日期时间格式错误，请指定正确的格式");
        } catch (NoMatchedTemplateException e2) {
            log.error("表头与模板不匹配");
            e2.printStackTrace();
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("表头与模板不匹配，请修改模板");
        }
    }

    @PostMapping()
    @ApiOperation(value = "导入图数据", notes = "导入图数据")
    @ApiResponses({@ApiResponse(code = 201, message = "CREATED")})
    public ResponseEntity importData(@RequestBody ImportGraphDataVO graphData) {
        Boolean isSuccess = graphDataService.importGraphData(graphData);
        if (isSuccess) {
            return ResponseEntity.status(HttpStatus.CREATED).body(true);
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("导入失败");
        }

    }

    @PostMapping("/text-data")
    @ApiOperation(value = "文本数据的关联", notes = "文本数据的关联，生成图数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "TextFileModelVO", name = "record", required = true)
    })
    @ApiResponses({@ApiResponse(code = 201, message = "CREATED")})
    public ResponseEntity importTextData(@RequestBody TextFileModelVO record) {
        try {
            return ResponseEntity.ok(graphDataService.generateGraphDataByText(record));
        } catch (DateTimeParseException e) {
            log.error("日期时间格式错误");
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("日期时间格式错误，请指定正确的格式");
        } catch (NoMatchedTemplateException e2) {
            log.error("表头与模板不匹配");
            e2.printStackTrace();
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("表头与模板不匹配，请修改模板");
        }
    }

    @ApiOperation(value = "任务概览")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "taskId", required = true)
    })
    @ApiResponse(code = 200, message = "ok", response = TaskOverviewVO.class)
    @GetMapping("/task-overview")
    public ResponseEntity<TaskOverviewVO> getTaskOverview(@RequestParam Integer taskId) {

        return ResponseEntity.ok(graphDataService.getTaskOverview(taskId));
    }

    @ApiOperation(value = "根据类别获取结点数据, 不传pageNum或pageSize 则获取全部")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "taskId", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "pageNum"),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "pageSize"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "label", required = true),
    })
    @ApiResponse(code = 200, message = "ok", response = GraphNeo4jVO.Node.class, responseContainer = "List")
    @GetMapping("/label-node")
    public ResponseEntity<List<GraphNeo4jVO.Node>> getNodeByLabelAndTaskId(@RequestParam Integer taskId,
                                                                           @RequestParam(required = false) Integer pageNum,
                                                                           @RequestParam(required = false) Integer pageSize,
                                                                           @RequestParam String label) {
        return ResponseEntity.ok(graphDataService.getNodeByLabelAndTaskId(taskId, pageNum, pageSize, label));
    }


    @ApiOperation(value = "任务数据用到的模型")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "taskId", required = true)
    })
    @ApiResponse(code = 200, message = "ok", response = String.class, responseContainer = "List")
    @GetMapping("/task-model")
    public ResponseEntity<List<String>> getModelContentByTaskId(@RequestParam Integer taskId) {

        return ResponseEntity.ok(graphDataService.getModelContentByTaskId(taskId));
    }

    @PostMapping("/doc-data")
    @ApiOperation(value = "文档数据的关联以及导入", notes = "文档数据的关联以及导入")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "DocImportVO", name = "record", required = true)
    })
    @ApiResponses({@ApiResponse(code = 201, message = "CREATED")})
    public ResponseEntity importGraphDataByDocFile(@RequestBody DocImportVO record) {

        return ResponseEntity.ok(graphDataService.importDocGraph(record));

    }

    @PostMapping("/kb-import")
    @ApiOperation(value = "引入知识库，这是个假接口", notes = "这是个假接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "fileName", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "taskId", required = true)
    })
    @ApiResponses({@ApiResponse(code = 201, message = "CREATED")})
    public ResponseEntity importKB(@RequestParam String fileName, @RequestParam Integer taskId) throws IOException {

        return ResponseEntity.ok(graphDataService.importKB(taskId, fileName));

    }

    @PostMapping("/test")
    @ApiOperation(value = "导入知识库，这是个假接口", notes = "这是个假接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "savedFileName", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "taskId", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "fileId", required = true),
    })
    @ApiResponses({@ApiResponse(code = 201, message = "CREATED")})
    public ResponseEntity test(@RequestParam String savedFileName, @RequestParam Integer taskId, @RequestParam Integer fileId) {

        return ResponseEntity.ok(graphDataService.test(savedFileName, taskId, fileId));

    }

    @PostMapping("/file-graph")
    @ApiOperation(value = "文件数据一键导入", notes = "文件数据一键导入")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "array", name = "fileIds", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "taskId", required = true)
    })
    @ApiResponses({@ApiResponse(code = 201, message = "CREATED")})
    public ResponseEntity importFileGraph(@RequestParam List<Integer> fileIds, @RequestParam Integer taskId) {

        return ResponseEntity.ok(graphDataService.autoImportFileGraph(fileIds, taskId));

    }
}
